import { handle_native_method } from "./utils";
import { serverUrl } from "./settings";
function request(option, boolean) {
  let reqObj = null,
    ajaxTimeId = null;
  let user =handle_native_method("getUserInfo")
  let loginId = user.loginId;
  let userId = user.userId;
  let gender=user.gender;
  let commonInfo = handle_native_method("getAppInfo");
  option.url =
    option.url +
    "gender=" +
    gender +
    "&loginId=" +
    loginId +
    "&userId=" +
    userId +
    "&platform=" +
    commonInfo.platform +
    "&appVersion=" +
    commonInfo.appVersion +
    "&appCode=" +
    commonInfo.appCode +
    "&channel=" +
    commonInfo.channel;

  if (boolean) {
  } else {
    reqObj = $.ajax({
      url: serverUrl + "/" + option.url,
      data: option.data || "",
      dataType: option.dataType || "json",
      type: option.type,
      success: e => {
        // 清除定时器
        clearTimeout(ajaxTimeId);
        option.callback(e);
      },
      error: err => {
        // handle_native_method("showErrorView");
        return;
      },
      contentType: option.contentType
        ? "application/json"
        : "application/x-www-form-urlencoded"
    });
    ajaxTimeId = setTimeout(() => {
      // 说明3秒钟 还没有请求成功, 则终止请求
      reqObj.abort();
      // 调用native 错误页面
      handle_native_method("showErrorView");
    }, 4000);
  }
}

export default request;
